package com.yanboo.service.impl;

import com.yanboo.domain.CodeColumnConfig;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.yanboo.repository.CodeColumnConfigRepository;
import com.yanboo.service.CodeColumnConfigService;
import com.yanboo.service.dto.CodeColumnConfigDto;
import com.yanboo.service.dto.CodeColumnConfigQueryCriteria;
import com.yanboo.service.mapstruct.CodeColumnConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author fdq
* @date 2023-11-23
**/
@Service
@RequiredArgsConstructor
public class CodeColumnConfigServiceImpl implements CodeColumnConfigService {

    private final CodeColumnConfigRepository codeColumnConfigRepository;
    private final CodeColumnConfigMapper codeColumnConfigMapper;

    @Override
    public PageResult<CodeColumnConfigDto> queryAll(CodeColumnConfigQueryCriteria criteria, Pageable pageable){
        Page<CodeColumnConfig> page = codeColumnConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(codeColumnConfigMapper::toDto));
    }

    @Override
    public List<CodeColumnConfigDto> queryAll(CodeColumnConfigQueryCriteria criteria){
        return codeColumnConfigMapper.toDto(codeColumnConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CodeColumnConfigDto findById(unknowType columnId) {
        CodeColumnConfig codeColumnConfig = codeColumnConfigRepository.findById(columnId).orElseGet(CodeColumnConfig::new);
        ValidationUtil.isNull(codeColumnConfig.getColumnId(),"CodeColumnConfig","columnId",columnId);
        return codeColumnConfigMapper.toDto(codeColumnConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(CodeColumnConfig resources) {
        codeColumnConfigRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CodeColumnConfig resources) {
        CodeColumnConfig codeColumnConfig = codeColumnConfigRepository.findById(resources.getColumnId()).orElseGet(CodeColumnConfig::new);
        ValidationUtil.isNull( codeColumnConfig.getColumnId(),"CodeColumnConfig","id",resources.getColumnId());
        codeColumnConfig.copy(resources);
        codeColumnConfigRepository.save(codeColumnConfig);
    }

    @Override
    public void deleteAll(unknowType[] ids) {
        for (unknowType columnId : ids) {
            codeColumnConfigRepository.deleteById(columnId);
        }
    }

    @Override
    public void download(List<CodeColumnConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CodeColumnConfigDto codeColumnConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" columnName",  codeColumnConfig.getColumnName());
            map.put(" columnType",  codeColumnConfig.getColumnType());
            map.put(" dateAnnotation",  codeColumnConfig.getDateAnnotation());
            map.put(" dictName",  codeColumnConfig.getDictName());
            map.put(" extra",  codeColumnConfig.getExtra());
            map.put(" formShow",  codeColumnConfig.getFormShow());
            map.put(" formType",  codeColumnConfig.getFormType());
            map.put(" keyType",  codeColumnConfig.getKeyType());
            map.put(" listShow",  codeColumnConfig.getListShow());
            map.put(" notNull",  codeColumnConfig.getNotNull());
            map.put(" queryType",  codeColumnConfig.getQueryType());
            map.put(" remark",  codeColumnConfig.getRemark());
            map.put(" tableName",  codeColumnConfig.getTableName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}